# -*- coding: utf-8 -*-
"""
@Time ： 2021/11/21 20:38
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：Web自动化基础
"""
# 导入selenium库
from selenium import webdriver
from selenium.webdriver.common.by import By
from time import sleep


# 打开浏览器（创建一个Chrome对象）
# taskkill /F /IM chromedriver.exe

driver = webdriver.Ie()
# 隐式等待：如果立即没找到元素，就等一秒再找，直到找到或者报错（整个浏览器生效）
driver.implicitly_wait(10)
# 在哪里打开网站？刚打开的浏览器里面打开
driver.get('http://testingedu.com.cn:8000/index.php/Home/user/login.html')
# 获取title
print(driver.title)
print(driver.current_url)

# selenium定位写法
ele = driver.find_element(By.TAG_NAME,'input')
# 输入
ele.send_keys('13800138006')

# 得到的是一个元素列表
eles = driver.find_elements(By.TAG_NAME,'input')
eles[1].send_keys('122345')
# 连写
driver.find_element(By.NAME,'verify_code').send_keys('1')
# 点击登录（IE点击无效）
# driver.find_element(By.CLASS_NAME,'J-login-submit').click()
ele = driver.find_element(By.CLASS_NAME,'J-login-submit')
driver.execute_script('arguments[0].click()',ele)

# 获取提示
sleep(1)
# msg = driver.find_element(By.CLASS_NAME,'layui-layer-content layui-layer-padding').text
msg = driver.find_element(By.XPATH,'//*[@id="layui-layer1"]/div[2]').text
print(msg)

# 刷新
driver.refresh()
# 清空
# driver.find_element(By.TAG_NAME,'input').clear()
# 输入用户名手写xpath
driver.find_element(By.XPATH,'//input[@id="username"]').send_keys('13800138006')
driver.find_element(By.XPATH,'//input[@id="password"]').send_keys('123456')
driver.find_element(By.XPATH,'//input[@id="verify_code"]').send_keys('1')
ele = driver.find_element(By.XPATH,'//*[@id="verify_code_img"]')
ele.screenshot('../lib/verify.png')
# text()用法：text()表示元素里面的文本，contains表示包含
ele = driver.find_element(By.XPATH,'//a[contains(text(),"登")]')
driver.execute_script("arguments[0].click()",ele)
username = driver.find_element(By.XPATH,'//a[@class="red userinfo"]').text
print(username)


# 按浏览器的返回键
# driver.back()
# 退出浏览器
driver.quit()
